package com.inspur.his.controller;

import com.inspur.his.pojo.PayStandard;
import com.inspur.his.service.PayStandardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;

@Controller
@RequestMapping("/payStandard")
public class payStandardController {

    private int pageSize = 10;

    @Autowired
    private PayStandardService payStandardService;

    /**
     * 跳转收费标准页面
     * @return
     */
    @RequestMapping("/toAdd")
    public String toAdd() {
        return "/outDepart/ChargeCriterionAdd.jsp";
    }

    /**
     * 跳转收费标准更新页面
     * @param model
     * @return
     */
    @RequestMapping("/toUpdate")
    public String toUpdateMakeRegister(Model model, Integer id) {
        PayStandard payStandard = payStandardService.getOneById(id);
        model.addAttribute("payStandard", payStandard);
        return "/outDepart/ChargeCriterionUpdate.jsp";
    }

    /**
     * 添加收费标准信息
     * @return
     */
    @RequestMapping("/add")
    public String add(PayStandard payStandard) {
        payStandardService.add(payStandard);
        return "redirect:/payStandard/show?currPage=1";
    }

    /**
     * 更新
     * @param payStandard
     * @return
     */
    @RequestMapping("/update")
    public String update(PayStandard payStandard) {
        payStandardService.update(payStandard);
        return "redirect:/payStandard/show?currPage=1";
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    public String delete(Integer id) {
        payStandardService.delete(id);
        return "redirect:/payStandard/show?currPage=1";
    }

    @RequestMapping("/pay")
    public String pay(Integer roId) {
        payStandardService.delete(roId);
        return "redirect:/payStandard/show?currPage=1";
    }

    /**
     * 分页查询
     * @param model
     * @param currPage
     * @return
     */
    @RequestMapping("/show")
    public String show(Model model, String code, int currPage) {
        int total = payStandardService.count(code);
        List<PayStandard> list = payStandardService.show(code, currPage, pageSize);
        int lastPage = 1;
        if (total>10) {
            lastPage = total % pageSize == 0 ? total/pageSize : total/pageSize + 1;
        }
        model.addAttribute("total", total);
        model.addAttribute("lastPage", lastPage);
        model.addAttribute("currPage", currPage);
        model.addAttribute("payStandards", list);
        return "/outDepart/ChargeCriterion.jsp";
    }
}
